package com.kime.jwt.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.kime.jwt.entity.User;
import com.kime.jwt.service.UserService;
import com.kime.jwt.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: JiangYi
 * @Date: 2022-03-22 22:53:59
 * @Description: 用户模块
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user/login")
    public Map<String, Object> login(User user) {
        log.info("用户名: 【{}】", user.getName());
        log.info("密码: 【{}】", user.getPassword());
        Map<String, Object> map = new HashMap<>();
        try {
            User userInfo = userService.login(user);
            Map<String, String> payload = new HashMap<>();
            payload.put("id", userInfo.getId());
            payload.put("name", userInfo.getName());
            // 生成 JWT 令牌
            String token = JWTUtils.generateToken(payload);
            map.put("state", true);
            map.put("msg", "认证成功");
            map.put("token", token); // 响应 token
        } catch (Exception e) {
            map.put("state", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    /*@PostMapping("/user/test")
    public Map<String, Object> test(String token) {
        log.info("当前 token 为：[{}]", token);
        Map<String, Object> map = new HashMap<>();
        try {
            DecodedJWT verify = JWTUtils.verifyToken(token);// 验证令牌
            map.put("state", true);
            map.put("msg", "请求成功!");
            return map;
        } catch (SignatureVerificationException e) {
            e.printStackTrace();
            map.put("msg", "无效签名!");
        } catch (TokenExpiredException e) {
            e.printStackTrace();
            map.put("msg", "token 过期!");
        } catch (AlgorithmMismatchException e) {
            e.printStackTrace();
            map.put("msg", "token 算法不一致!");
        } catch (InvalidClaimException e) {
            e.printStackTrace();
            map.put("msg", "失效的 payload!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "其它异常!");
        }
        map.put("state", false);
        return map;
    }*/

    @PostMapping("/user/test")
    public Map<String, Object> test(HttpServletRequest request) {
        String token = request.getHeader("token");
        log.info("当前 token 为：[{}]", token);
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        String id = tokenInfo.getClaim("id").asString();
        String name = tokenInfo.getClaim("name").asString();
        log.info("用户 id：[{}]", id);
        log.info("用户 name：[{}]", name);
        Map<String, Object> map = new HashMap<>();
        // 处理业务逻辑
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("id", id);
        dataMap.put("name", name);
        map.put("state", true);
        map.put("msg", "请求成功!");
        map.put("data", dataMap);
        return map;
    }
}
